home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / b_stack.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  1.6 KB  |  82 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  b_stack.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_BSTACK_H
  16. #define LEDA_BSTACK_H
  17.  
  18. //------------------------------------------------------------------------------
  19. // bounded stacks 
  20. //------------------------------------------------------------------------------
  21.  
  22. #include <LEDA/basic.h>
  23.  
  24.  
  25.  
  26. template<class type> 
  27.  
  28. class _CLASSTYPE b_stack
  29. {
  30.     type* first;  // start of array
  31.     type* stop;   // one position behind last element
  32.         type* free;   // pointer to first free element
  33.  
  34. public:
  35.  
  36. b_stack(int n)
  37. {
  38. #if !defined(LEDA_CHECKING_OFF)
  39.   if (n<1) error_handler(99,"b_stack: bad size");
  40. #endif
  41.   free = first = new type[n];
  42.   stop = first + n;
  43.   if (first==0) error_handler(99,"b_stack: out of memory");
  44.  }
  45.  
  46. virtual ~b_stack() { delete [] first; }
  47.  
  48. int   size()  const { return free - first; }
  49.  
  50. int   empty() const { return (free==first) ? true : false; }
  51.  
  52. void push(const type& a)
  53. {
  54. #if !defined(LEDA_CHECKING_OFF)
  55.   if (free==stop) error_handler(99,"b_stack overflow");
  56. #endif
  57.   *free++ = a;
  58. }
  59.  
  60. type pop()
  61. {
  62. #if !defined(LEDA_CHECKING_OFF)
  63.   if (free==first) error_handler(99,"b_stack underflow");
  64. #endif
  65.   return *--free;
  66. }
  67.  
  68. type top() const 
  69. {
  70. #if !defined(LEDA_CHECKING_OFF)
  71.   if (free==first) error_handler(99,"b_stack empty");
  72. #endif
  73.   return *(free-1);
  74. }
  75.  
  76. void clear() { free = first; }
  77.  
  78. };
  79.  
  80. #endif
  81.